//
//  UTLoginHttpKit.m
//  MyApplication
//
//  Created by yons on 16/11/8.
//  Copyright © 2016年 DongXiaoWei. All rights reserved.
//

#import "UTLoginHttpKit.h"
#import <UIKit/UIKit.h>
#import "UTUserSign.h"

@implementation UTLoginHttpKit

+(void)POSTWithURL:(NSString *)url param:(NSDictionary *)param header:(NSDictionary *)header callBack:(LoginCallBack)callBack
{
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    request.HTTPMethod = @"POST";
    
    //请求头
    NSDictionary * headDic = [self getSTHeadDictionary:header params:param strurl:url];
    
    for (NSString *key in [headDic allKeys])
    {
        NSString *value = [headDic objectForKey:key];
        [request setValue:value forHTTPHeaderField:key];
    }
    
    //参数
    if(param)
    {
        NSMutableString * mutableString = [NSMutableString string];
        for (NSString * key in [param allKeys])
        {
            NSString *value = [param objectForKey:key];
            [mutableString appendString:[NSString stringWithFormat:@"%@=%@&",key,value]];
        }
        [mutableString deleteCharactersInRange:NSMakeRange([mutableString length]-1, 1)];
        NSData *data = [mutableString dataUsingEncoding:NSUTF8StringEncoding];
        request.HTTPBody = data;
    }
    
    //请求
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *sessionDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        callBack(response,data,error);
        
//        NSHTTPURLResponse * httpResponce = (NSHTTPURLResponse *)response;
//        if (httpResponce.statusCode == 404)
//        {
//            callBack(response,nil,error);
//        }
//        else
//        {
//            callBack(response,data,error);
//        }
        
    }];
    [sessionDataTask resume];
}


+(NSDictionary *)getSTHeadDictionary:(NSDictionary *)header params:(NSDictionary *)dic_params strurl:(NSString*)url{
    
    @try {
        //时间
        NSString *time = [UTUserSign getSystemTime];
        
        //生成sign
        NSString *strSign = [UTUserSign sign:dic_params time:time];
        
        NSString *sign = strSign;
        NSMutableDictionary *head_dic = [[NSMutableDictionary alloc] initWithCapacity:5];
        NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
        NSString* versionNum =[infoDict objectForKey:@"CFBundleVersion"];
        NSString *version = [NSString stringWithFormat:@"%@.%@",[infoDict objectForKey:@"CFBundleShortVersionString"],versionNum];
        NSString *appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleNameKey];
        if (appName == nil) {
            appName =@"";
        }
        NSString *deviceModel = [[UIDevice currentDevice] model];
        NSString *deviceSys = [[UIDevice currentDevice] systemVersion];
        CGFloat screenScale = [[UIScreen mainScreen] scale];
        NSString *user_agent = [NSString stringWithFormat:@"%@/%@ (%@; iOS %@; Sc ale/%0.2f)",appName,version,deviceModel,deviceSys,screenScale];
        [head_dic setObject:sign forKey:@"cas-client-sign"];
        [head_dic setObject:time forKey:@"cas-client-time"];
        [head_dic setObject:url forKey:@"cas-client-service"];//@"http://app.utouu"
        [head_dic setObject:user_agent forKey:@"User-Agent"];
        [head_dic setObject:@"UTOUU-ST-INVALID" forKey:@"cas-client-st"];
        for (NSString *key in header) {
            NSLog(@"key: %@ value: %@", key, header[key]);
            if([key isEqualToString:@"st"]|| [key isEqualToString:@"cas-client-st"]){
                [head_dic setObject:header[key] forKey:@"cas-client-st"];
            }
        }
        return head_dic;
    }
    @catch (NSException *exception) {
        
    }
    @finally {
        
    }
}

@end
